package com.team.teamuserservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.team.teamuserservice.entity.dto.VehicleDTO;
import com.team.teamuserservice.entity.po.BizUserVehicle;
import com.team.teamuserservice.entity.vo.VehicleVO;
import com.team.teamuserservice.mapper.BizUserVehicleMapper;
import com.team.teamuserservice.service.IBizUserVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.teamuserservice.uitls.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户车辆信息 服务实现类
 * </p>
 *
 * @author cxs
 * @since 2025-07-09
 */
@Service
public class BizUserVehicleServiceImpl extends ServiceImpl<BizUserVehicleMapper, BizUserVehicle> implements IBizUserVehicleService {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    /**
     * 添加车辆信息
     * @param brand
     * @param model
     * @param plateNumber
     * @param file
     * @param id
     * @return
     */
    @Override
    public Boolean addCar(String brand, String model, String plateNumber, MultipartFile file, Long id) throws Exception {

        // 上传图片
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());

        BizUserVehicle bizUserVehicle = new BizUserVehicle()
                .setUserId(id)
                .setPlateNumber(plateNumber)
                .setBrand(brand)
                .setModel(model)
                .setVehicleUrl(url)
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());

        int i = baseMapper.insert(bizUserVehicle);

        return i > 0;


    }

    @Override
    public List<VehicleVO> select(Long id) {
        List<VehicleVO> list = lambdaQuery()
                .eq(BizUserVehicle::getUserId, id)
                .select()
                .list()
                .stream()
                .map(car -> BeanUtil.copyProperties(car, VehicleVO.class))
                .collect(Collectors.toList());

        return list;
    }
}
